﻿@using Octokit
@inject GithubService GithubService
@attribute [JSCustomElement]
@inherits NextTickComponentBase

@if (HasOwnerAndRepo)
{
    <div>
        <MAutocomplete @bind-Value="_search"
                       Items="@_releases"
                       Dark
                       TItem="Release"
                       TItemValue="long"
                       TValue="long?"
                       ItemValue="r => r.Id"
                       ItemText="r => r.Name"
                       Class="mb-4"
                       HideDetails="true"
                       Dense
                       Flat
                       Label="Select Release version"
                       PrependInnerIcon="mdi-text-box-search-outline"
                       Solo
                       OnSelectedItemUpdate="item => _selected = item">
            <ItemContent>
                <MListItemIcon>
                    <MIcon>mdi-tag-outline</MIcon>
                </MListItemIcon>
                <MListItemContent>
                    <MListItemTitle>@context.Item.Name</MListItemTitle>
                    <MListItemSubtitle>@context.Item.PublishedAt</MListItemSubtitle>
                </MListItemContent>
            </ItemContent>
        </MAutocomplete>

        <MCard Dark="Dark" MinHeight="180" Outlined Loading="@_loading">
            @if (_selected is not null)
            {
                <div class="d-flex">
                    <MListItem>
                        <MListItemAvatar Size="48">
                            <MImage Src="@_selected.Author.AvatarUrl"></MImage>
                        </MListItemAvatar>
                        <MListItemContent>
                            <MListItemTitle Class="mb-1 text-h6">
                                <MI18n Key="released-by">
                                    <PlaceholderContent>
                                        @if (context == 0)
                                        {
                                            <AppLink Href="@_selected.Author.HtmlUrl" Content="@_selected.Author.Login" />
                                        }
                                    </PlaceholderContent>
                                </MI18n>
                            </MListItemTitle>
                            <MListItemSubtitle>
                                <MI18n Key="published-on">
                                    <PlaceholderContent>
                                        @if (context == 0)
                                        {
                                            <strong>@_selected.PublishedAt</strong>
                                        }
                                    </PlaceholderContent>
                                </MI18n>
                            </MListItemSubtitle>
                        </MListItemContent>
                    </MListItem>
                </div>

                <MDivider />

                <div class="pa-4" style="@MdStyle">
                    <MMarkdownIt Source="@_selected.Body"
                                 Scope="desc"
                                 Class="release-note"
                                 Linkify>
                    </MMarkdownIt>
                </div>
            }
        </MCard>
    </div>
}
else
{
    <div>Failed to fetch releases.</div>
}

@code {

    [SupplyParameterFromQuery(Name = "v")]
    [Parameter] public string? Version { get; set; }

    [Parameter, EditorRequired]
    public string? Owner { get; set; }

    [Parameter, EditorRequired]
    public string? Repo { get; set; }

    [Parameter]
    public bool Dark { get; set; }

    [Parameter]
    public bool IsIntro { get; set; }

    private bool _loading = true;
    private long? _search;
    private Release? _selected;

    private List<Release> _releases = new();

    private bool HasOwnerAndRepo => !string.IsNullOrWhiteSpace(Owner) && !string.IsNullOrWhiteSpace(Repo);

    private string? MdStyle => IsIntro ? "max-height:400px; overflow-y: auto" : null;

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);

        if (firstRender && HasOwnerAndRepo)
        {
            var releases = await GithubService.FetchReleasesAsync(Owner!, Repo!);
            _releases = releases.Where(u => u.Prerelease == false).ToList();
            UpdateSelected();
            _loading = false;

            StateHasChanged();
        }
    }

    protected override void OnParametersSet()
    {
        if (HasOwnerAndRepo && _releases.Count > 0)
        {
            UpdateSelected();
        }
    }

    private void UpdateSelected()
    {
        _selected = _releases.FirstOrDefault(u => u.Name == Version) ?? _releases.FirstOrDefault();
        _search = _selected?.Id;
    }

}
